(function(){
	//Private variables
	var socket,
		connectionURL = 'http://localhost:20000'; //Default value

	//Insert socket.io script in page
	var headID = document.getElementsByTagName("head")[0];         

	//Inserto la librería Socket.io
	var socketIo = document.createElement('script');
	socketIo.type = 'text/javascript';
	socketIo.onload=scriptLoaded;
	socketIo.src = connectionURL + '/socket.io/socket.io.js';
	headID.appendChild(socketIo);

	function scriptLoaded () {
		socket = io.connect(connectionURL);
	}

	//Tickmap
	var tickmap = {
		//Connection URL Setter
		setConnectionURL: function(url) {
			connectionURL = url;
		},

		//Categories CRUD functions
		categories: {
			getAllShort: function(callback) {
				socket.emit('getAllCategories', true, callback);
			},
			getAll: function(callback) {
				socket.emit('getAllCategories', false, callback);
			},
			find: function(filters, callback) {
				socket.emit('findCategories', filters, callback);
			},
			findOne: function(filters, callback) {
				socket.emit('findOneCategory', filters, callback);
			},
			save: function(category, callback) {
				socket.emit('saveCategory', category, callback);
			},
			remove: function(category, callback) {
				socket.emit('removeCategory', category, callback);
			}
		},

		places: {
			getAllShort: function(callback) {
				socket.emit('getAllPlaces', true, callback);
			},
			getAll: function(callback) {
				socket.emit('getAllPlaces', false, callback);
			},
			find: function(filters, callback) {
				socket.emit('findPlaces', filters, callback);
			},
			findOne: function(filters, callback) {
				socket.emit('findOnePlace', filters, callback);
			},
			save: function(place, geo, callback) {
				socket.emit('savePlace', {place: place, geo: geo}, callback);
			},
			remove: function(place, callback) {
				socket.emit('removePlace', place, callback);
			}
		}

	};
	if(!window.tickmap){window.tickmap=tickmap;}//We create a shortcut for our framework
})();